package cn.edu.scau.cmi.pengjiajian.client;

import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.util.Set;

import cn.edu.scau.cmi.pengjiajian.dao.Operation;
import cn.edu.scau.cmi.pengjiajian.dao.Student;
import cn.edu.scau.cmi.pengjiajian.dao.StudentDAO;
import cn.edu.scau.cmi.pengjiajian.dao.Teacher;
import cn.edu.scau.cmi.pengjiajian.dao.TeacherDAO;

public class HibernateClient extends Application{
	static int chooseKind = 1;
	static Long id = null;
	static Stage stage = new Stage();
	double maxX = 1; //用于将窗口动态居中
	double maxY = 1; //用于将窗口动态居中
	
	static int choose = 1; //用于管理员模式选择学生还是教师
	
	protected Scene getSceneAdmOperation(int kind) {
		Label labId = new Label("帐号：");
		labId.setFont(new Font("Serif",20));
		TextField textId = new TextField();
		textId.setFont(Font.font("Times",15));
		
		if(kind == 1) {
			labId.setVisible(false);
			textId.setVisible(false);
		}
		
		HBox hbox1 = new HBox(20);
		hbox1.getChildren().addAll(labId, textId);
		hbox1.setAlignment(Pos.TOP_CENTER);
		
		Label labName = new Label("名字：");
		labName.setFont(new Font("Serif",20));
		TextField textName = new TextField();
		textName.setFont(Font.font("Times",15));
		if(kind == 3) {
			labName.setVisible(false);
			textName.setVisible(false);
		}
		
		HBox hbox2 = new HBox(20);
		hbox2.getChildren().addAll(labName, textName);
		hbox2.setAlignment(Pos.TOP_CENTER);
		
		ToggleGroup group = new ToggleGroup();
		RadioButton rbstu = new RadioButton("学生");
		RadioButton rbtea = new RadioButton("教师");
		rbstu.setSelected(true);
		rbstu.setFont(new Font("Serif",20));
		rbstu.setToggleGroup(group);
		choose = 1;
		rbstu.setOnAction(e ->{
			if(rbstu.isSelected()) choose = 1;
		});
		rbtea.setFont(new Font("Serif",20));
		rbtea.setToggleGroup(group);
		rbtea.setOnAction(e ->{
			if(rbtea.isSelected()) choose = 2;
		});
		
		HBox hbox3 = new HBox(30);
		hbox3.getChildren().addAll(rbstu, rbtea);
		hbox3.setAlignment(Pos.TOP_CENTER);
		
		Button btn = new Button();
		btn.setFont(new Font("Serif",20));
		if(kind == 1)
			btn.setText("添加");
		else if(kind == 2)
			btn.setText("修改");
		else
			btn.setText("删除");
		
		VBox vbox = new VBox(20);
		vbox.getChildren().addAll(hbox1, hbox2, hbox3, btn);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(80,70,50,70));
		
		Scene scene = new Scene(vbox);
		
		btn.setOnAction(e->{
			String temp = textId.getText();
			String name = textName.getText();
			boolean isOK = false;
			if(kind == 1 && name != null) { //选择添加
				if(choose == 1) //管理员选择操作学生
					Operation.addStudent(1L, name);
				else
					Operation.addTeacher(1L, name);
				isOK = true;
			}
			else if(kind == 2 || kind == 3) {
				try {
					id = Long.valueOf(temp);
					System.out.println(id);
				}
				catch (Exception ex) {
					System.out.println("输入有误");
					id = null;
				}
				if(id != null){
					if(kind == 3) { //选择删除
						isOK = true;
						if(choose == 1) //管理员选择操作学生
							Operation.deleteStudent(id, "");
						else
							Operation.deleteTeacher(id, "");
					}
					else if(name != null){
						isOK = true;
						if(choose == 1) //管理员选择操作学生
							Operation.updateStudent(id, name);
						else
							Operation.updateTeacher(id, name);
					}
				}
			}
			if(isOK) {
				stage.setScene(getSceneMainAdmin());
				stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
				stage.setY(maxY / 2 - stage.getHeight() / 2);
			}
		});
		
		return scene;
	}
	
	protected Scene getSceneMainAdmin() {
		Button btnAdd = new Button("添加");
		Button btnAmend = new Button("修改");
		Button btnDele = new Button("删除");
		Button btnReturn = new Button("返回");
		btnAdd.setFont(new Font("Serif",20));
		btnAmend.setFont(new Font("Serif",20));
		btnDele.setFont(new Font("Serif",20));
		btnReturn.setFont(new Font("Serif",20));
		
		Label labTip = new Label("管理员模式");
		labTip.setFont(new Font("Serif",30));
		
		
		VBox vbox = new VBox(30);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(30,70,50,70));
		vbox.getChildren().addAll(labTip, btnAdd, btnAmend, btnDele, btnReturn);
		
		Scene scene = new Scene(vbox);
		
		btnAdd.setOnAction(e->{
			stage.setScene(getSceneAdmOperation(1));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		btnAmend.setOnAction(e->{
			stage.setScene(getSceneAdmOperation(2));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		btnDele.setOnAction(e->{
			stage.setScene(getSceneAdmOperation(3));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		btnReturn.setOnAction(e->{
			stage.setScene(getSceneLogin());
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		
		return scene;
	}
	
	protected Scene getSceneLogin() {
		Label labId = new Label("帐号：");
		labId.setFont(new Font("Serif",20));
		TextField textId = new TextField();
		textId.setFont(Font.font("Times",15));
		
		HBox hboxTop = new HBox(20);
		hboxTop.getChildren().addAll(labId, textId);
		hboxTop.setAlignment(Pos.TOP_CENTER);
		
		ToggleGroup group = new ToggleGroup();
		RadioButton rbstu = new RadioButton("学生");
		RadioButton rbtea = new RadioButton("教师");
		RadioButton rbadm = new RadioButton("管理员");
		rbstu.setSelected(true);
		rbstu.setFont(new Font("Serif",20));
		rbstu.setToggleGroup(group);
		rbstu.setOnAction(e ->{
			if(rbstu.isSelected()) {
				chooseKind = 1;
				Operation.people = chooseKind;
			}
		});
		rbtea.setFont(new Font("Serif",20));
		rbtea.setToggleGroup(group);
		rbtea.setOnAction(e ->{
			if(rbtea.isSelected()) {
				chooseKind = 2;
				Operation.people = chooseKind;
			}
		});
		rbadm.setFont(new Font("Serif",20));
		rbadm.setToggleGroup(group);
		rbadm.setOnAction(e ->{
			if(rbadm.isSelected()) {
				chooseKind = 3;
				Operation.people = chooseKind;
			}
		});
		chooseKind = 1;
		Operation.people = chooseKind;
		
		HBox hboxMid = new HBox(30);
		hboxMid.getChildren().addAll(rbstu, rbtea, rbadm);
		hboxMid.setAlignment(Pos.TOP_CENTER);
		
		Button btnlogin = new Button("登录");
		btnlogin.setFont(new Font("Serif",20));
		
		Label labTip = new Label("帐号不存在");
		labTip.setVisible(false);
		labTip.setFont(new Font("Serif",15));
		
		VBox vbox = new VBox(20);
		vbox.getChildren().addAll(hboxTop, hboxMid, btnlogin, labTip);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(80,70,50,70));
		
		Scene scene = new Scene(vbox);
		
		btnlogin.setOnAction(e->{
			//读取id框的值
			String temp = textId.getText();
			try {
				id = Long.valueOf(temp);
				System.out.println(id);
			}
			catch (Exception ex) {
				if(chooseKind != 3)
					System.out.println("输入不符合要求");
				id = null;
			}
			
			if(id == null && chooseKind != 3) {
				labTip.setVisible(true);
			}
			else {
				boolean isOK = false;
				if(chooseKind == 1) {
					StudentDAO studentDAO = new StudentDAO();
					Student student = studentDAO.findById(id);
					if(student == null)
						labTip.setVisible(true);
					else isOK = true;
				}
				else if(chooseKind == 2) {
					TeacherDAO teacherDAO = new TeacherDAO();
					Teacher teacher = teacherDAO.findById(id);
					if(teacher == null)
						labTip.setVisible(true);
					else isOK = true;
				}
				else isOK = true;
				if(isOK){
					labTip.setVisible(false);
					//跳转
					if(chooseKind == 1)
						stage.setScene(getSceneTeaAndStuFace(1));
					else if(chooseKind == 2)
						stage.setScene(getSceneTeaAndStuFace(2));
					else
						stage.setScene(getSceneMainAdmin());
					stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
					stage.setY(maxY / 2 - stage.getHeight() / 2);
				}
			}
		});
		
		return scene;
		
	}
	
	//kind为1，表示选择老师，为2表示选择学生
	protected Scene getSceneAdmendTeaAndStu(int kind) {
		Label labTip = new Label();
		if(kind == 1) labTip.setText("选择老师");
		else labTip.setText("选择学生");
		labTip.setFont(new Font("Serif",32));
		
		Label labId = new Label("帐号：");
		labId.setFont(new Font("Serif",20));
		TextField textId = new TextField();
		textId.setFont(Font.font("Times",15));
		
		HBox hbox1 = new HBox(20);
		hbox1.getChildren().addAll(labId, textId);
		hbox1.setAlignment(Pos.TOP_CENTER);
		
		Button btn = new Button("确认");
		btn.setFont(new Font("Serif",20));
		
		Label lab = new Label("请正确填写信息");
		labTip.setFont(new Font("Serif",32));
		lab.setVisible(false);
		
		VBox vbox = new VBox(20);
		vbox.getChildren().addAll(labTip, hbox1, btn, lab);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(80,70,50,70));
		
		Scene scene = new Scene(vbox);
		
		btn.setOnAction(e->{
			String temp = textId.getText();
			boolean isOK = false;
			Long nowId = null;
			try {
				nowId = Long.valueOf(temp);
				System.out.println(id);
			}
			catch (Exception ex) {
				System.out.println("输入有误");
			}
			if(nowId != null){
				isOK = true;
				if(kind == 1)
					Operation.stuChooseTea(id, nowId);
				else
					Operation.teaChooseStu(id, nowId);
			}
			if(!isOK) {
				System.out.println("输入有误或者帐号不存在");
			}
			stage.setScene(getSceneTeaAndStuFace(kind));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		
		return scene;
	}
	//kind为1，学生模式，为2表示教师模式
	protected Scene getSceneTeaAndStuFace(int kind) {
		Button btnAmend = new Button("查看信息");
		Button btnChoose = new Button();
		if(kind == 1) btnChoose.setText("选择老师");
		else btnChoose.setText("选择学生");
		Button btnReturn = new Button("返回");
		btnAmend.setFont(new Font("Serif",20));
		btnChoose.setFont(new Font("Serif",20));
		btnReturn.setFont(new Font("Serif",20));
		
		Label labTip = new Label();
		if(kind == 1) labTip.setText("学生模式(id:)" + id);
		else labTip.setText("教师模式(id:)" + id);
		labTip.setFont(new Font("Serif",28));
		
		
		VBox vbox = new VBox(30);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(30,70,50,70));
		vbox.getChildren().addAll(labTip,btnAmend, btnChoose, btnReturn);
		
		Scene scene = new Scene(vbox);
		
		btnAmend.setOnAction(e->{
			stage.setScene(getSceneCheck(kind));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		btnChoose.setOnAction(e->{
			stage.setScene(getSceneAdmendTeaAndStu(kind));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		btnReturn.setOnAction(e->{
			stage.setScene(getSceneLogin());
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		
		return scene;
		
	}
	
	protected Scene getSceneCheck(int kind) {
		StudentDAO studentDAO = new StudentDAO();
		Student student = studentDAO.findById(id);
		TeacherDAO teacherDAO = new TeacherDAO();
		Teacher teacher = teacherDAO.findById(id);
		Label labTip = new Label("查看信息");
		labTip.setFont(new Font("Serif",32));
		
		Label labId = new Label("帐号：");
		labId.setFont(new Font("Serif",20));
		Label labIdInfo = new Label("" + id);
		labIdInfo.setFont(Font.font("Times",20));
		
		HBox hbox1 = new HBox(20);
		hbox1.getChildren().addAll(labId, labIdInfo);
		hbox1.setAlignment(Pos.TOP_CENTER);
		
		Label labName = new Label("名字：");
		labName.setFont(new Font("Serif",20));
		Label labNameInfo = new Label();
		if(kind == 1)
			labNameInfo.setText("" + student.getName());
		else
			labNameInfo.setText("" + teacher.getName());
		labNameInfo.setFont(Font.font("Times",20));
		
		HBox hbox2 = new HBox(20);
		hbox2.getChildren().addAll(labName, labNameInfo);
		hbox2.setAlignment(Pos.TOP_CENTER);
		
		Label labChoose = new Label();
		if(kind == 1)
			labChoose.setText("选择的教师");
		else
			labChoose.setText("选择的学生");
		labChoose.setFont(new Font("Serif",20));
		Label[] labshow = new Label[4];
		for(int i = 0; i < 4; i++) {
			labshow[i] = new Label();
		}
		
		if(kind == 1) {
			Teacher tmp = student.getTeacher();
			if(tmp != null)
				labshow[0].setText("帐号：" + tmp.getId() +
					"\n名字：" + tmp.getName());
		}
		else {
			Set<Student> set = teacher.getStudents();
			int i = 0;
			for(Student stu: set) {
				if(i >= 4) break;
				labshow[i].setText("帐号：" + stu.getId() +
						"\n名字：" + stu.getName());
				i++;
			}
		}
		
		Button btn = new Button("确认");
		btn.setFont(new Font("Serif",20));
		
		VBox vbox = new VBox(20);
		vbox.getChildren().addAll(labTip, hbox1, hbox2, labChoose, labshow[0],
				labshow[1], labshow[2], labshow[3], btn);
		vbox.setAlignment(Pos.TOP_CENTER);
		vbox.setPadding(new Insets(80,70,50,70));
		
		Scene scene = new Scene(vbox);
		
		btn.setOnAction(e->{
			stage.setScene(getSceneTeaAndStuFace(kind));
			stage.setX(maxX / 2 - stage.getWidth() / 2); //进行居中显示
			stage.setY(maxY / 2 - stage.getHeight() / 2);
		});
		
		return scene;
	}
	
	public void start(Stage primaryStage) {
		Screen screen = Screen.getPrimary();
		Rectangle2D bounds = screen.getVisualBounds();
		maxX = bounds.getMaxX(); //用于将窗口动态居中
		maxY = bounds.getMaxY(); //用于将窗口动态居中
		
		
		stage.setScene(getSceneLogin());
		stage.setTitle("实验七：Hibernate");
		stage.show();
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}

}

	